package com.up.vms.interfaces.rest.utils;

import java.util.concurrent.TimeUnit;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IdleConnectionMonitorThread extends Thread {
    private static final Logger log = LoggerFactory.getLogger(IdleConnectionMonitorThread.class);
    private final PoolingHttpClientConnectionManager connMgr;
    private volatile boolean shutdown;
    private int timeout = 30;
    private long interval = 6000L;

    IdleConnectionMonitorThread(PoolingHttpClientConnectionManager connMgr) {
        this.connMgr = connMgr;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public void setInterval(long interval) {
        this.interval = interval;
    }

    public void run() {
        while(true) {
            try {
                if (!this.shutdown) {
                    synchronized(this) {
                        this.wait(this.interval);
                        this.connMgr.closeExpiredConnections();
                        this.connMgr.closeIdleConnections((long)this.timeout, TimeUnit.SECONDS);
                        continue;
                    }
                }
            } catch (InterruptedException var4) {
                log.error("监控http connection的空闲线程 出现异常：" + var4.getMessage(), var4);
                Thread.currentThread().interrupt();
            }

            return;
        }
    }

    public void shutdown() {
        this.shutdown = true;
        synchronized(this) {
            this.notifyAll();
        }
    }
}

